//
//  InvestAccountViewController.m
//  WBBaseProject
//
//  Created by xiaozhi on 2018/4/2.
//  Copyright © 2018年 吴斌. All rights reserved.
//

#import "InvestAccountViewController.h"
#import "NSObject+AFNetworking.h"

//#import "ManageMoneyCell.h"
#import "HomeSectionTitle.h"
//#import "BorrowOrInvestCell.h"
#import "ActivateBorrowView.h"
//#import "PlatformDataCell.h"
#import "HomeFootView.h"
//#import "LoginViewController.h"
#import "XYHomeNavigationView.h"
#import "InvestReportViewController.h"

#import "SettingCell.h"
#import "AccountMoneyView.h"
#import "ShowInvestCell.h"
#import "MyBidRecordCell.h"
#import "NewerHeaderView.h"
#import "TimeDetailCell.h"

//首页导航栏滚动距离
#define NAVBAR_CHANGE_POINT 50

static NSString * const ShowInvestCellReuseId = @"ShowInvestCell";
static NSString * const MyBidRecordCellReuseId = @"MyBidRecordCell";
static NSString * const TimeDetailCellReuseId = @"TimeDetailCell";

//static NSString * const BorrowOrInvestCellReuseId = @"BorrowOrInvestCell";
//static NSString * const CycleScrollViewCellReuseId = @"CycleScrollViewCell";
//static NSString * const PlatformDataCellReuseId = @"PlatformDataCell";

@interface InvestAccountViewController()<UITableViewDelegate,UITableViewDataSource,ActivateBorrowViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) ActivateBorrowView *headView;

/** <#注释#> */
@property (nonatomic, strong) UIView *footView;
/** <#注释#> */
@property (nonatomic, strong) XYHomeNavigationView *naviView;

@end

@implementation InvestAccountViewController

#pragma mark - 生命周期 Life Circle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"投资账户";
    [self registerCell];
    [self.view addSubview:self.tableView];
    self.navigationController.navigationBar.hidden = YES;
    
    XYHomeNavigationView *naviView = [[XYHomeNavigationView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kNavigationBarHeight)];
    _naviView = naviView;
    _naviView.titleLb.text = @"投资账户";
    [_naviView.rightBtn removeFromSuperview];
    [_naviView.leftBtn addTarget:self action:@selector(pop) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:naviView];
}
- (void)pop
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)registerCell
{
    [self.tableView registerNib:[UINib nibWithNibName:@"ShowInvestCell" bundle:nil] forCellReuseIdentifier:ShowInvestCellReuseId];
    [self.tableView registerNib:[UINib nibWithNibName:@"MyBidRecordCell" bundle:nil] forCellReuseIdentifier:MyBidRecordCellReuseId];
    [self.tableView registerNib:[UINib nibWithNibName:@"TimeDetailCell" bundle:nil] forCellReuseIdentifier:TimeDetailCellReuseId];
//    [self.tableView registerClass:[BorrowOrInvestCell class] forCellReuseIdentifier:BorrowOrInvestCellReuseId];
//    [self.tableView registerClass:[PlatformDataCell class] forCellReuseIdentifier:PlatformDataCellReuseId];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    switch (section) {
        case 0:
            return 1;
            break;
        case 1:
            return 2;
            break;
        case 2:
            return 3;
            break;
        default:
            return 0;
            break;
    }
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    switch (indexPath.section) {
        case 0:
        {
            ShowInvestCell *cell = [tableView dequeueReusableCellWithIdentifier:ShowInvestCellReuseId forIndexPath:indexPath];
            return cell;
            break;
        }
        case 1:
        {
            TimeDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:TimeDetailCellReuseId forIndexPath:indexPath];
            return cell;
            break;
        }
        case 2:
        {
            MyBidRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:MyBidRecordCellReuseId forIndexPath:indexPath];
            return cell;
            break;
        }
        default:
            return nil;
            break;
    }
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    switch (section) {
        case 0:
        {
            return nil;
            break;
        }
        case 1:
        {
            NewerHeaderView *view = [[NSBundle mainBundle] loadNibNamed:@"NewerHeaderView" owner:nil options:nil].firstObject;
            
            return view;
            break;
        }
        case 2:
        {
            HomeSectionTitle *view = [[NSBundle mainBundle] loadNibNamed:@"HomeSectionTitle" owner:nil options:nil].firstObject;
            
            view.titleLb.text = @"我的投标";
            
            UIView *line = [[UIView alloc]init];
            line.backgroundColor = WB_HEX_COLOR(0xf0f0f0);
            [view addSubview:line];
            [line mas_makeConstraints:^(MASConstraintMaker *make) {
                make.bottom.right.equalTo(0);
                make.left.equalTo(15);
                make.height.equalTo(0.5);
            }];
            
            view.frame = CGRectMake(0, 0, kScreenWidth, 30);
            return view;
            break;
        }
        default:
            return nil;
            break;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            return 0;
            break;
        case 1:
            return 120;
            break;
        case 2:
            return 80;
            break;
        default:
            return 0;
            break;
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    if (indexPath.section == 0) {
//        if (indexPath.row == 0) {
//            return 72.0f;
//        }else{
//            return 125.0f;
//        }
//    }
    if (indexPath.section == 1) {
        return 40;
    }else{
        return 90.0f;
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        InvestReportViewController *vc = [[InvestReportViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat cellOffy = scrollView.contentOffset.y;
    if (cellOffy < 0) {
        self.naviView.hidden = YES;
    }else{
        self.naviView.hidden = NO;
    }
    
    if (cellOffy > NAVBAR_CHANGE_POINT) {
        
        [self.naviView changeColorWithState:YES];
        self.naviView.backgroundColor = WB_RGBAColor(255, 255, 255, 0.8);
        [WB_SharedApplication setStatusBarStyle:UIStatusBarStyleDefault animated:true];
    }else {
        CGFloat alpa = cellOffy/NAVBAR_CHANGE_POINT;
        if (alpa > 0.8) {
            self.naviView.backgroundColor = WB_RGBAColor(255, 255, 255, 0.8);
            return ;
        }
        [self.naviView changeColorWithState:NO];
        self.naviView.backgroundColor = WB_RGBAColor(255, 255, 255, alpa);
        [WB_SharedApplication setStatusBarStyle:UIStatusBarStyleLightContent animated:true];
    }
}

#pragma mark - ActiveView Delegate
- (void)clickBorrowView:(ActivateBorrowView *)view button:(UIButton *)button
{
    if (![Factory isLogin]) {
        [Factory loadLoginScreen];
    }else{
        //登录后进入进件页面
    }
}

#pragma mark - 懒加载  Lazy Load
- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
        _tableView.backgroundColor = WB_RGBColor(245, 248, 253);
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = self.footView;
        _tableView.tableHeaderView = self.headView;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

- (ActivateBorrowView *)headView
{
    if (_headView == nil) {
        _headView = [[ActivateBorrowView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 240)];
        [_headView.InfoView removeFromSuperview];
        
        AccountMoneyView* vi = [[NSBundle mainBundle] loadNibNamed:@"AccountMoneyView" owner:self options:nil].firstObject;
        vi.backgroundColor = [UIColor clearColor];
        //[vi.immediateActiveBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        
        vi.ing = ^{
            
        };
        vi.overdue = ^{
            
        };
        
        [_headView.whiteBg addSubview:vi];
        [vi mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.equalTo(0);
            make.bottom.equalTo(-20);
        }];
        _headView.delegate = self;
    }
    return _headView;
}

- (UIView *)footView
{
    if (_footView == nil) {
        
        _footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 60)];
        _footView.backgroundColor = [UIColor whiteColor];
        
        UIButton *btn = [[UIButton alloc]init];
        btn.frame = CGRectMake(0, 0, kScreenWidth, 60);
        [btn setTitle:@"查看全部" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(shaowAll) forControlEvents:UIControlEventTouchUpInside];
        
        [_footView addSubview:btn];
        
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 0.5)];
        line.backgroundColor =WB_HEX_COLOR(0xf0f0f0);
        [_footView addSubview:line];
    }
    return _footView;
}
- (void)shaowAll
{
    
}


@end
